package jdmx.framework.common.serialization;

import jdmx.library.da.Pager;

import org.jdom.Element;

public class XmlSerializerForPager implements XmlSerializer {

	@Override
	public Element serialize(Object envelopeItem, String elementName) {
		Pager pager = (Pager) envelopeItem;
		if (pager == null) return null;
		
		Element xmlPager = new Element(elementName);
		if (pager.pageSize != null) {
			Element xmlPageSize = new Element("PageSize");
			xmlPageSize.setText(pager.pageSize.toString());
			xmlPager.addContent(xmlPageSize);	
		}
		if (pager.pageNo != null) {
			Element xmlPageNo = new Element("PageNo");
			xmlPageNo.setText(pager.pageSize.toString());
			xmlPager.addContent(xmlPageNo);
		}
		if (pager.recordCount != null) {
			Element xmlRecordCount = new Element("RecordCount");
			String recordCountString = pager.recordCount.toString();
			xmlRecordCount.setText(recordCountString);
			xmlPager.addContent(xmlRecordCount);		
		}
		return xmlPager;
	}

	@Override
	public Pager deserialize(Element element) {
		Pager pager = new Pager();
		if (element.getChild("PageSize") != null) {
			String pageSize =  element.getChild("PageSize").getText();
			pager.pageSize = Integer.parseInt(pageSize);
		}
		
		if (element.getChild("PageNo") != null) {
			String pageNo =  element.getChild("PageNo").getText();
			pager.pageNo = Integer.parseInt(pageNo);
		}
		
		if (element.getChild("RecordCount") != null) {
			String recordCount =  element.getChild("RecordCount").getText();
			pager.recordCount = Integer.parseInt(recordCount);		
		}
		return pager;
	}

}
